package main

import (
	"fmt"

	"github.com/gogo/protobuf/proto"

	"ch03.protobuf/address"
)

func tryfuncMarshal() {
	msg := address.Person{
		Id:   1,
		Name: "john",
		Gender: address.GenderType_FEMALE,
	}
	buf, err := msg.Marshal()
	if err != nil {
		fmt.Printf("[marshal] err: [%v]\n", err)
		return
	}
	fmt.Printf("[marshal] succ, get right struct:[%v]\n", buf)

	unmarshalSt := &address.Person{}
	err = unmarshalSt.Unmarshal(buf)
	if err != nil {
		fmt.Printf("[marshal] err: [%v]\n", err)
		return
	}
	fmt.Printf("[unmarshal] succ, result: [%+v], type: [%T]\n", unmarshalSt, unmarshalSt)
}

func tryMarshal() {

	msg := address.Person{
		Id:   1,
		Name: "john",
		Gender: address.GenderType_FEMALE,
	}
	buf, err := proto.Marshal(&msg)
	if err != nil {
		fmt.Printf("[marshal] err: [%v]\n", err)
		return
	}
	fmt.Printf("[marshal] succ, get right struct:[%v]\n", buf)

	unmarshalSt := &address.Person{}
	err = proto.Unmarshal(buf, unmarshalSt)
	if err != nil {
		fmt.Printf("[marshal] err: [%v]\n", err)
		return
	}
	fmt.Printf("[unmarshal] succ, result: [%+v], type: [%T]\n", unmarshalSt, unmarshalSt)
}

func main() {
	tryMarshal()
	tryfuncMarshal()
}
